package com.scedu.reflection;
//通过反射获取泛型

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public class Test10 {
    public void test01(Map<String,User> map, List<User> list){
        System.out.println("test01");
    }
    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        //处理第一个函数
        Method mtest01 = Test10.class.getDeclaredMethod("test01", Map.class, List.class);
        Type[] genericParameterTypes = mtest01.getGenericParameterTypes(); //通过方法获取泛型参数类型集合
        for (Type genericParameterType : genericParameterTypes) {
            System.out.println("--------");
            System.out.println(genericParameterType);
            //如果参数类型属于一个参数化类型

            if(genericParameterType instanceof ParameterizedType){
                System.out.println("======");
                //强转后，，获取真实类型
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }
        }
        System.out.println("处理第二个函数");
        //处理第二个函数
        Method mtest02 = Test10.class.getDeclaredMethod("test02",null);
        Type genericReturnType = mtest02.getGenericReturnType();
        System.out.println(genericReturnType);
        if(genericReturnType instanceof ParameterizedType){
            System.out.println("======");
            //强转后，，获取真实类型
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument);
            }
        }
    }
}
